from django.db import models
from django.contrib.auth.models import AbstractUser #继承 AbstractUser
from datetime import datetime


class UserProfile(AbstractUser):
    """
    用户表
    """
    name = models.CharField(max_length=32, null=True, blank=True, verbose_name="姓名")
    avator = models.ImageField(upload_to="avator/", verbose_name="用户头像", null=True, blank=True,default="default.jpg",help_text="头像信息")
    birthday = models.DateField(null=True, blank=True, verbose_name="出生年月",help_text="出生年月日")
    gender = models.CharField(max_length=16, choices=(("male", u"男"), ("female", "女")), default="female", verbose_name="性别",help_text="性别")
    email = models.EmailField(max_length=100, null=True, blank=True, verbose_name="邮箱",help_text="邮箱")
    mobile = models.CharField(max_length=100,null=True,blank=True,verbose_name="手机号",help_text="手机号")
    usertype = models.CharField(max_length=32, choices=(("teacher", u"老师"), ("student", u"学生")), default="student", verbose_name="用户类型",help_text="用户类型")
    openid = models.CharField(max_length=64,verbose_name='openid',default="")
    avator_url = models.CharField(max_length=128, verbose_name='微信头像的url', default="")


    class Meta:
        verbose_name = "用户"
        verbose_name_plural = verbose_name


class Grade(models.Model):
    """
    班级表
    """
    GradeName = models.CharField(max_length=32, null=True, blank=True, verbose_name="班级名")
    teacher = models.ForeignKey(UserProfile, verbose_name='讲师', null=True, blank=True, on_delete=models.CASCADE,related_name="grade")
    create_time = models.DateTimeField(default=datetime.now, verbose_name="创建时间")

    class Meta:
        verbose_name = "班级管理"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.GradeName

    __repr__ = __str__


class GradeStudent(models.Model):
    create_time = models.DateTimeField(default=datetime.now, verbose_name="创建时间")
    grade = models.ForeignKey(Grade,on_delete=models.CASCADE, verbose_name="班级名",null=True, blank=True,help_text="所属班级",related_name="GradeStudent")
    user = models.ForeignKey(UserProfile, verbose_name='学生', null=True, blank=True, on_delete=models.CASCADE,related_name="grades")

    class Meta:
        verbose_name = "班级与学生管理"
        verbose_name_plural = verbose_name
        unique_together = ("user","grade")


class VerifyCode(models.Model):
    """
    短信验证码
    """
    code = models.CharField(max_length=10, verbose_name="验证码")
    mobile = models.CharField(max_length=11, verbose_name="电话")
    add_time = models.DateTimeField(default=datetime.now, verbose_name="添加时间")

    class Meta:
        verbose_name = "短信验证码"
        verbose_name_plural = verbose_name

    def __repr__(self):
        return self.code

    __str__ = __repr__

